package flexo.file.reader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import flexo.common.LogMsgType;
import flexo.file.Node;

public abstract class Reader
{
	/**
	 * Build a node structure (using fileNode as the root-node) from a file.
	 */
	public abstract void read (Node fileNode);
	
	protected void formatError(String file, int line)
	{
		flexo.Kernel.log("flexo.graphic.reader.Reader", "Format incorrect in '"+file+"'  in line "+line+": "+line, LogMsgType.ERROR);
	}
	
	protected BufferedReader getBufferedReader(Node fileNode)
	{
		try
		{
			flexo.common.System sys = flexo.Kernel.getSystem(flexo.file.FileSystem.ID);
			if (sys != null)
			{		
				flexo.file.FileSystem fs = (flexo.file.FileSystem)sys;
				return fs.getBufferedReader(fileNode.name);
			}
			else
			{
				return new BufferedReader(new InputStreamReader(new FileInputStream(fileNode.name)));
			}		
		}
		catch (IOException ioe)
		{
			flexo.Kernel.log("flexo.graphic.reader.Reader", "Couldn't find '" + fileNode.name + "'", LogMsgType.ERROR);
			return null;
		}		
	}
}
